package cn.com.tom.dao.impl;

import cn.com.tom.bean.Company;
import cn.com.tom.bean.Project;
import cn.com.tom.dao.ProjectDao;
import cn.com.tom.util.Dbs;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProjectDaoImpl extends BaseDaoImpl<Project> implements ProjectDao {

    private static String GET_SQL;
    private static String QUERY_SQL;
    private static RowMapper<Project> ROW_MAPPER;

    static {
        QUERY_SQL = "SELECT t1.id, t1.name,t1.intro,t1.website,t1.image,t1.begin_day,t1.end_day," +
                "t2.id,t2.intro,t2.logo,t2.name,t2.website " +
                "FROM project t1 LEFT JOIN company t2 " +
                "ON t1.company_id=t2.id ";
        GET_SQL = QUERY_SQL + " WHERE t1.id=?";
        ROW_MAPPER = new RowMapper<Project>() {
            @Override
            public Project mapRow(ResultSet resultSet, int i) throws SQLException {
                Project project = new Project();
                project.setId(Integer.valueOf(resultSet.getInt("t1.id")));
                project.setName(resultSet.getString("t1.name"));
                project.setImage(resultSet.getString("t1.image"));
                project.setBeginDay(resultSet.getDate("t1.begin_day"));
                project.setEndDay(resultSet.getDate("t1.end_day"));
                project.setIntro(resultSet.getString("t1.intro"));
                project.setWebsite(resultSet.getString("t1.website"));

                Company company = new Company();
                company.setId(Integer.valueOf(resultSet.getInt("t2.id")));
                company.setName(resultSet.getString("t2.name"));
                company.setWebsite(resultSet.getString("t2.website"));
                company.setLogo(resultSet.getString("t2.logo"));
                company.setIntro(resultSet.getString("t2.intro"));
                project.setCompany(company);

                return project;
            }
        };
    }

    @Override
    public boolean save(Project bean) {
        Integer id = bean.id;
        String sql = "";
        List<Object> args = new ArrayList<>();
        args.add(bean.getName());
        args.add(bean.getIntro());
        args.add(bean.getWebsite());

        args.add(bean.getImage());
        args.add(bean.getBeginDay());
        args.add(bean.getEndDay());
        args.add(bean.getCompany().id);

        if (id == null || id < 1) {
            sql = "INSERT INTO project(name,intro,website,image,begin_day,end_day,company_id) VALUES(?,?,?,?,?,?,?)";
        } else {
            sql = "UPDATE project SET name=?,intro=?,website=?,image=?,begin_day=?,end_day=?,company_id=? WHERE id=?";
            args.add(id);
        }
        return Dbs.save(sql, args.toArray());
    }

    @Override
    public Project get(Integer id) {
        return Dbs.queryForObject(GET_SQL, ROW_MAPPER, id);
    }

    @Override
    public List<Project> list() {
        return Dbs.list(QUERY_SQL, ROW_MAPPER);
    }

}
